package com.spzx.product.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.ProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productUnit")
@Tag(name = "商品单位管理模块")
public class ProductUnitController extends BaseController {

    @Resource
    private  ProductUnitService productUnitService;

    @Operation(summary = "条件查询分页数据")
    @GetMapping("list")
    public TableDataInfo list(ProductUnit productUnit)
    {
       /* startPage();
        //   List<Brand> list = roleService.selectRoleList(role);
        List<Brand> list = productUnitService.selectRoleList(productUnit);
        return getDataTable(list);*/
       // Page<ProductUnit> page = productUnitService.selectUnitPage(productUnit);
        Page<ProductUnit> page = productUnitService.selectUnitPage(productUnit);
        return new TableDataInfo(page.getRecords(), (int)page.getTotal());
    }

    @Operation(summary = "查询所有")
    @GetMapping
    public AjaxResult listAll(){
        return success(productUnitService.list());
    }

    @Operation(summary = "根据id查询详情")
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable Long id){
        return success(productUnitService.getById(id));
    }

    @Operation(summary = "修改")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ProductUnit productUnit){
        if(!productUnitService.checkUnitNameUnique(productUnit)){
            return error("更新商品单位：" + productUnit.getName() + "失败，商品单位名称已存在");
        }
        productUnit.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(productUnitService.updateById(productUnit));
    }


    @Operation(summary = "新增")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ProductUnit productUnit){
        if (!productUnitService.checkUnitNameUnique(productUnit)){
            return error("新增商品单位：" + productUnit.getName() + "失败，商品单位名称已存在");
        }
        productUnit.setCreateBy(SecurityUtils.getUsername());
        return toAjax(productUnitService.save(productUnit));
    }

    @Operation(summary = "批量删除")
    @DeleteMapping("{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids){
        return toAjax(productUnitService.removeBatchByIds(ids));
    }
}
